<?php

class HelperDate {
	
	/**
	 * Return mysql date based on a date in french form.
	 * 
	 * @param $date Date dd/mm/yyyy or dd/mm/yy 
	 * @throws CustomExceptionBadFormat when format is not valid
	 */
	public static function dateFrToMysql($date) {
		if( preg_match('/^[0-9]{2}\/[0-9]{2}\/((1|2)[0-9]{3}|[0-9]{2})$/',$date) ) {
			$date = explode('/',$date);
			
			//manage year with only 2 digits : "10" means "2010".
			if( $date[2] < 100 ) {
				$date[2] += 2000;
			}
			
			return $date[2].'-'.$date[1].'-'.$date[0];
		}
		else {
			throw new CustomExceptionBadFormat("$date : Format de date incorrect");
		}
	}
	
	/**
	 * Return a date in french form based on a mysql date.
	 * 
	 * @param $date Date yyyy-MM-dd hh:mm:ss
	 * @throws CustomExceptionBadFormat when format is not valid
	 */
	public static function dateMysqlToFr($date) {
		if( preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$/',$date) ) {
			$date = explode('-',$date);
			$day = explode(' ',$date[2]);
			return $day[0].'/'.$date[1].'/'.$date[0];
		}
		else {
			throw new CustomExceptionBadFormat("$date : Format de date incorrect");
		}
	}
	
	
	
}
?>